DAMN = (typeof DAMN != "undefined")?  DAMN : {};

DAMN.Search = new function() 
{   
  this.AttachTo = function(parent) {
    parent.tabs('add', '#tabs-Search', "Search");
    this.tab = $("#tabs-Search");

    var self = this;  
               
    var pa = $('<form style="height:90%;">\
                <div class="container">\
                  <div class="content">\
                    <div style="position: relative; margin-top:4px; margin-bottom:4px;"> \
                      <input id="Search-search" name="search" style="width:99%;" class="ui-widget ui-state-default ui-widget-header"/> \
                      <span style="position: absolute; top: 0; right: 0; margin-right: 5px; cursor: pointer;" class="ui-icon ui-icon-search"></span> \
                    </div> \
                  </div> \
                  <div class="content row" style="height: 30%;">\
                    <div id="All-Tags" class="content"></div><div class="list"><div id="Search-Tags"></div></div>\
                  </div>\
                  <div class="content row" style="height: 30%;">\
                    <div id="All-Formats" class="content"></div><div class="list"><div id="Search-Formats"></div></div>\
                  </div>\
                  <div class="content row" style="height: 30%;">\
                    <div id="All-Collections" class="content"></div><div class="list"><div id="Search-Collections"></div></div>\
                  </div>\
                </div></form>');
    this.tab.css('height', '100%');
    this.tab.append(pa);
    
    pa.find('.content').addClass('ui-widget ui-widget-content');
    
    var changeFunction = function() {
  	 if (!this.checked) {
  	   pa.find('input[value="All"][name="'+$(this).attr('name')+'"]').attr('checked', this.checked);
     } else {
        if (pa.find('input[checked][name="'+$(this).attr('name')+'"]').length == pa.find('input[name="'+$(this).attr('name')+'"]').length-1) {
          pa.find('input[value="All"][name="'+$(this).attr('name')+'"]').attr('checked', true);
        }
     }
		};
    
    this.CreateInputs(pa, '#Search-Tags', '/tags/', '#All-Tags', '#All-Tags input', 'tags', 'tags');
    
    this.CreateInputs(pa, '#Search-Formats', '/formats/', '#All-Formats', '#All-Formats input', 'formats', 'formats');
    
    this.CreateInputs(pa, '#Search-Collections', '/collections/', '#All-Collections', '#All-Collections input', 'collections', 'collections');

    //alert(pa.find('span.ui-icon-search').length);
  	pa.find('span.ui-icon-search').click(function () {
  	 var tab = $('#tabs-Content-Main').data('Content');
  	 var url = '/assets/search/?'+pa.serialize();
     DAMN.Log.debug('DAMN.Search: searching: '+url); 
  	 tab.SetDataURL(url);
  	 tab.Load(); 
    });
    
    pa.find('#Search-search').keypress(function(event) {
      if (event.keyCode == '13') {                  
         event.preventDefault();
         pa.find('span.ui-icon-search').click();
       }
    });
  
  };
  
                /*<span style="float:left;width: 16px; height: 16px; margin-right:6px;">\
                <input t="'+type+'" name="'+type+'" value="'+data.id+'" type="checkbox"/>\
              </span>\*/
  
  this.CreateInput = function(type, data) {
    var i = '<p>\
            <input type="checkbox" name="'+type+'" value="'+data.id+'" id="'+type+data.id+'" /> <label for="'+type+data.id+'">'+ (data.name?data.name:data.type) +'</label>';
    i += (typeof data.amount != "undefined")?'<span style="position:absolute;top:5px;right:0.5em">('+data.amount+')</span></p>':'</p>';    
    return $(i);
  };
  
  
  this.CreateInputs = function(pa, containerWin, url, allWin, input, type, types) {
    var self = this;
    var changeFunction = function() {
  	 if (!this.checked) {
  	   pa.find('input[value="All"][name="'+$(this).attr('name')+'"]').attr('checked', this.checked);
     } else {
        if (pa.find('input[checked][name="'+$(this).attr('name')+'"]').length == pa.find('input[name="'+$(this).attr('name')+'"]').length-1) {
          pa.find('input[value="All"][name="'+$(this).attr('name')+'"]').attr('checked', true);
        }
     }
		};
		
    var tags = pa.find(containerWin);
    tags.addClass('loading-small');
    //pa.find('#All-Tags').parent().hide();
    $.getJSON(url, function(data) {
      tags.empty();
      tags.removeClass('loading-small');
      //pa.find('#All-Tags').parent().show();
      
      var category = null;
      for(m in data){
        if (category != data[m].category) {
          category = data[m].category;
          tags.append($('<table style="width:100%;"><tr><td style="width:0; padding-right: 0.25em;">'+category+'</td><td><div style="margin-bottom: 0.25em; width: 100%; height:1px; background-color:white;"/></td></tr></table>')); 
        }
        var f = self.CreateInput(type, data[m]);
        tags.append(f);
        f.find('input').change(changeFunction);
      }
      
      pa.find(allWin).html(self.CreateInput(type, {'id':'All', 'name':'All ('+data.length+' '+types+')'}));
      pa.find(input).click(function() {
          tags.find('input[type="checkbox"]').attr('checked', this.checked);
        }
      );
  	});
  };
  
  	
};

